﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using Saluse.MediaKit;
using System.Collections.ObjectModel;


namespace OpenLightGroup.Visualizers
{
    /// <summary>
    /// Object used to provide the sound sample values of song as it plays.
    /// </summary>
    public class SoundBite
    {
        //private short numberOfGroups = 3;

        private ReadOnlyCollection<byte> _frequencyChannels;
        private ReadOnlyCollection<short> _samples;
       

        public SoundBite()
        {

        }


        public SoundBite(int samplesPerSecond, short[] samples)
        {

            this.SetFrequencyChannels(samplesPerSecond, samples);

        }

        public SoundBite(IRawMediaStreamSource source, short[] samples)
        {
  
            this.SetFrequencyChannels(source.SamplesPerSecond, samples);
            
        }

        //public SoundBite(IRawMediaStreamSource source, short[] samples)
        //{


        //    byte[] frequencyPeaks = PeakMeter.CalculateFrequencies
        //        (samples, source.SamplesPerSecond);

        //    this._frequencyChannels = new ReadOnlyCollection<byte>(frequencyPeaks);
        //    this._samples = new ReadOnlyCollection<short>(samples);


        //    var numberOfSamplesPerGroup = frequencyPeaks.Count() / numberOfGroups;


        //    //var topLow = from i in frequencyPeaks
        //    //                 .Take(numberOfSamplesPerGroup)
        //    //             orderby i descending
        //    //             select i;

        //    var topLow = from i in frequencyPeaks
        //                     .Take(2)
        //                 orderby i descending
        //                 select i;

        //    byte low = topLow.FirstOrDefault();

        //    if (low != 0)
        //        this.Low = low; //(short.MaxValue / low);
        //    else
        //        this.Low = 0;


        //    var topMid = from i in frequencyPeaks.Skip(numberOfSamplesPerGroup + 1)
        //                     .Take(numberOfSamplesPerGroup)
        //                 orderby i descending
        //                 select i;

        //    byte mid = topMid.FirstOrDefault();

        //    if (mid != 0)
        //        this.Mid = mid; //(short.MaxValue / topMid.FirstOrDefault());
        //    else
        //        this.Mid = 0;

        //    var topHigh = from i in frequencyPeaks.Skip((numberOfSamplesPerGroup * 2) + 1)
        //                     .Take(numberOfSamplesPerGroup)
        //                  orderby i descending
        //                  select i;

        //    byte high = topHigh.FirstOrDefault();

        //    if (high != 0)
        //        this.High = high;  //(short.MaxValue / topHigh.FirstOrDefault());
        //    else
        //        this.High = 0;

        //}

        //public SoundBite(short[] samples)
        //{                       

        //    var numberOfSamplesPerGroup = samples.Count() / numberOfGroups;


        //    var topLow = from i in samples
        //                     .Take(numberOfSamplesPerGroup)
        //                 orderby i descending
        //                 select i;

        //    short low = topLow.FirstOrDefault();

        //    if (low != 0)
        //        this.Low = (short.MaxValue / low);
        //    else
        //        this.Low = 0;


        //    var topMid = from i in samples.Skip(numberOfSamplesPerGroup + 1)
        //                     .Take(numberOfSamplesPerGroup)
        //                 orderby i descending
        //                 select i;

        //    short mid = topMid.FirstOrDefault();

        //    if (mid != 0)
        //        this.Mid = (short.MaxValue / topMid.FirstOrDefault());
        //    else
        //        this.Mid = 0;

        //    var topHigh = from i in samples.Skip((numberOfSamplesPerGroup * 2) + 1)
        //                     .Take(numberOfSamplesPerGroup)
        //                  orderby i descending
        //                  select i;

        //    short high = topHigh.FirstOrDefault();

        //    if (high != 0)
        //        this.High = (short.MaxValue / topHigh.FirstOrDefault());
        //    else
        //        this.High = 0;


        //}


        public int SamplesPerSecond { get; private set; }


        /// <summary>
        /// Provides a list of 19 channels that contain the values of each sound
        ///     frequency of a given sound sample.
        /// </summary>
        public ReadOnlyCollection<byte> FrequencyChannels
        {
            get
            {
                return _frequencyChannels;
            }
        }

        /// <summary>
        /// The exact samples taken from a song as it plays.
        /// </summary>
        public ReadOnlyCollection<short> Samples
        {
            get
            {
                return _samples;
            }
        }

        //public byte Low { get; set; }
        //public byte Mid { get; set; }
        //public byte High { get; set; }

        private void SetFrequencyChannels(int samplesPerSecond, short[] samples)
        {
            this.SamplesPerSecond = samplesPerSecond;

            byte[] frequencyPeaks = PeakMeter.CalculateFrequencies
                            (samples, samplesPerSecond);

            this._frequencyChannels = new ReadOnlyCollection<byte>(frequencyPeaks);
            this._samples = new ReadOnlyCollection<short>(samples);
        }

    }

}
